package view;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.UnknownHostException;

import javax.swing.JFrame;

import controller.ClientControler;

public class LoginView {

	private ClientControler controlleur;

	private JFrame frame;
	private javax.swing.JButton jButtonConnexionOK;
	private javax.swing.JButton jButtonConnexionQuitter;
	private javax.swing.JButton jButtonCreerCompte;
	private javax.swing.JLabel jLabelConnnexion;
	private javax.swing.JLabel jLabelMotDePasse;
	private javax.swing.JLabel jLabelPseudo;
	private javax.swing.JPanel jPanelBoutonsConnexion;
	private javax.swing.JPanel jPanelConnexion;
	private javax.swing.JPanel jPanelPseudoMotDePasse;
	private javax.swing.JPasswordField jPasswordFieldMotDePasse;
	private javax.swing.JTextField jTextFieldPseudo;

	public void showView() {
		this.frame.setVisible(true);
	}
	
	public void closeView() {
		this.frame.setVisible(false);
	}

	/**
	 * Create the application.
	 */
	public LoginView(ClientControler controlleur) {
		this.controlleur = controlleur;
		initialize();
	}

	private void initialize() {
		frame = new JFrame();
		jPanelConnexion = new javax.swing.JPanel();
		jLabelConnnexion = new javax.swing.JLabel();
		jPanelPseudoMotDePasse = new javax.swing.JPanel();
		jLabelPseudo = new javax.swing.JLabel();
		jTextFieldPseudo = new javax.swing.JTextField();
		jLabelMotDePasse = new javax.swing.JLabel();
		jPasswordFieldMotDePasse = new javax.swing.JPasswordField();
		jButtonCreerCompte = new javax.swing.JButton();
		jPanelBoutonsConnexion = new javax.swing.JPanel();
		jButtonConnexionOK = new javax.swing.JButton();
		jButtonConnexionQuitter = new javax.swing.JButton();
		
//		frame.getContentPane().setFont(new Font("Dialog", Font.BOLD, 12));
//		frame.setBounds(100, 100, 450, 300);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		jLabelConnnexion.setFont(new java.awt.Font("Dialog", 0, 48)); // NOI18N
		jLabelConnnexion
				.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
		jLabelConnnexion.setText("Connexion");
		jLabelConnnexion
				.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);

		javax.swing.GroupLayout jPanelConnexionLayout = new javax.swing.GroupLayout(
				jPanelConnexion);
		jPanelConnexion.setLayout(jPanelConnexionLayout);
		jPanelConnexionLayout.setHorizontalGroup(jPanelConnexionLayout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addComponent(jLabelConnnexion,
						javax.swing.GroupLayout.DEFAULT_SIZE, 437,
						Short.MAX_VALUE));
		jPanelConnexionLayout.setVerticalGroup(jPanelConnexionLayout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(
						jPanelConnexionLayout.createSequentialGroup()
								.addComponent(jLabelConnnexion)
								.addContainerGap(28, Short.MAX_VALUE)));

		jLabelPseudo.setText("pseudo :");

		// jTextFieldPseudo.addActionListener(new
		// java.awt.event.ActionListener() {
		// public void actionPerformed(java.awt.event.ActionEvent evt) {
		// jTextFieldPseudoActionPerformed(evt);
		// }
		// });

		jLabelMotDePasse.setText("mot de passe :");

		jButtonCreerCompte.setForeground(new java.awt.Color(-16776961, true));
		jButtonCreerCompte.setText("<HTML><U>créer un compte</U></HTML>");
		jButtonCreerCompte.setBorderPainted(false);
		
		jButtonCreerCompte.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                controlleur.showNouveauCompteView();
            }
        });

		javax.swing.GroupLayout jPanelPseudoMotDePasseLayout = new javax.swing.GroupLayout(
				jPanelPseudoMotDePasse);
		jPanelPseudoMotDePasse.setLayout(jPanelPseudoMotDePasseLayout);
		jPanelPseudoMotDePasseLayout
				.setHorizontalGroup(jPanelPseudoMotDePasseLayout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								jPanelPseudoMotDePasseLayout
										.createSequentialGroup()
										.addGap(54, 54, 54)
										.addGroup(
												jPanelPseudoMotDePasseLayout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.LEADING,
																false)
														.addGroup(
																jPanelPseudoMotDePasseLayout
																		.createSequentialGroup()
																		.addComponent(
																				jLabelMotDePasse)
																		.addGap(18,
																				18,
																				18)
																		.addComponent(
																				jPasswordFieldMotDePasse,
																				javax.swing.GroupLayout.PREFERRED_SIZE,
																				148,
																				javax.swing.GroupLayout.PREFERRED_SIZE))
														.addGroup(
																jPanelPseudoMotDePasseLayout
																		.createSequentialGroup()
																		.addComponent(
																				jLabelPseudo)
																		.addPreferredGap(
																				javax.swing.LayoutStyle.ComponentPlacement.RELATED,
																				javax.swing.GroupLayout.DEFAULT_SIZE,
																				Short.MAX_VALUE)
																		.addComponent(
																				jTextFieldPseudo,
																				javax.swing.GroupLayout.PREFERRED_SIZE,
																				148,
																				javax.swing.GroupLayout.PREFERRED_SIZE)))
										.addContainerGap(118, Short.MAX_VALUE))
						.addGroup(
								javax.swing.GroupLayout.Alignment.TRAILING,
								jPanelPseudoMotDePasseLayout
										.createSequentialGroup()
										.addContainerGap(219, Short.MAX_VALUE)
										.addComponent(
												jButtonCreerCompte,
												javax.swing.GroupLayout.PREFERRED_SIZE,
												javax.swing.GroupLayout.DEFAULT_SIZE,
												javax.swing.GroupLayout.PREFERRED_SIZE)
										.addGap(90, 90, 90)));
		jPanelPseudoMotDePasseLayout
				.setVerticalGroup(jPanelPseudoMotDePasseLayout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								jPanelPseudoMotDePasseLayout
										.createSequentialGroup()
										.addContainerGap()
										.addGroup(
												jPanelPseudoMotDePasseLayout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.BASELINE)
														.addComponent(
																jLabelPseudo)
														.addComponent(
																jTextFieldPseudo,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE))
										.addGap(18, 18, 18)
										.addGroup(
												jPanelPseudoMotDePasseLayout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.BASELINE)
														.addComponent(
																jLabelMotDePasse)
														.addComponent(
																jPasswordFieldMotDePasse,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE))
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED,
												javax.swing.GroupLayout.DEFAULT_SIZE,
												Short.MAX_VALUE)
										.addComponent(
												jButtonCreerCompte,
												javax.swing.GroupLayout.PREFERRED_SIZE,
												javax.swing.GroupLayout.DEFAULT_SIZE,
												javax.swing.GroupLayout.PREFERRED_SIZE)));

		jButtonConnexionOK.setText("OK");
		jButtonConnexionOK.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e){
				controlleur.retrievePseudoPassword(jTextFieldPseudo.getText(), new String(jPasswordFieldMotDePasse.getPassword()));

            }
        });

		jButtonConnexionQuitter.setText("Quitter");
		jButtonConnexionQuitter.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                controlleur.annulerConnexion();
            }
        });

		javax.swing.GroupLayout jPanelBoutonsConnexionLayout = new javax.swing.GroupLayout(
				jPanelBoutonsConnexion);
		jPanelBoutonsConnexion.setLayout(jPanelBoutonsConnexionLayout);
		jPanelBoutonsConnexionLayout
				.setHorizontalGroup(jPanelBoutonsConnexionLayout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								jPanelBoutonsConnexionLayout
										.createSequentialGroup()
										.addGap(42, 42, 42)
										.addComponent(
												jButtonConnexionOK,
												javax.swing.GroupLayout.PREFERRED_SIZE,
												90,
												javax.swing.GroupLayout.PREFERRED_SIZE)
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED)
										.addComponent(
												jButtonConnexionQuitter,
												javax.swing.GroupLayout.PREFERRED_SIZE,
												90,
												javax.swing.GroupLayout.PREFERRED_SIZE)
										.addContainerGap(16, Short.MAX_VALUE)));
		jPanelBoutonsConnexionLayout
				.setVerticalGroup(jPanelBoutonsConnexionLayout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								jPanelBoutonsConnexionLayout
										.createSequentialGroup()
										.addGap(46, 46, 46)
										.addGroup(
												jPanelBoutonsConnexionLayout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.BASELINE)
														.addComponent(
																jButtonConnexionOK)
														.addComponent(
																jButtonConnexionQuitter))
										.addContainerGap(25, Short.MAX_VALUE)));

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
				frame.getContentPane());
		frame.getContentPane().setLayout(layout);
		layout.setHorizontalGroup(layout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addComponent(jPanelConnexion,
						javax.swing.GroupLayout.DEFAULT_SIZE,
						javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
				.addComponent(jPanelPseudoMotDePasse,
						javax.swing.GroupLayout.DEFAULT_SIZE,
						javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
				.addGroup(
						javax.swing.GroupLayout.Alignment.TRAILING,
						layout.createSequentialGroup()
								.addContainerGap()
								.addComponent(jPanelBoutonsConnexion,
										javax.swing.GroupLayout.PREFERRED_SIZE,
										javax.swing.GroupLayout.DEFAULT_SIZE,
										javax.swing.GroupLayout.PREFERRED_SIZE)));
		layout.setVerticalGroup(layout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(
						layout.createSequentialGroup()
								.addContainerGap()
								.addComponent(jPanelConnexion,
										javax.swing.GroupLayout.PREFERRED_SIZE,
										javax.swing.GroupLayout.DEFAULT_SIZE,
										javax.swing.GroupLayout.PREFERRED_SIZE)
								.addPreferredGap(
										javax.swing.LayoutStyle.ComponentPlacement.RELATED)
								.addComponent(jPanelPseudoMotDePasse,
										javax.swing.GroupLayout.PREFERRED_SIZE,
										javax.swing.GroupLayout.DEFAULT_SIZE,
										javax.swing.GroupLayout.PREFERRED_SIZE)
								.addPreferredGap(
										javax.swing.LayoutStyle.ComponentPlacement.RELATED,
										javax.swing.GroupLayout.DEFAULT_SIZE,
										Short.MAX_VALUE)
								.addComponent(jPanelBoutonsConnexion,
										javax.swing.GroupLayout.PREFERRED_SIZE,
										javax.swing.GroupLayout.DEFAULT_SIZE,
										javax.swing.GroupLayout.PREFERRED_SIZE)));

		frame.pack();
	}

	
	public javax.swing.JPasswordField getjPasswordFieldMotDePasse() {
		return jPasswordFieldMotDePasse;
	}

	public void setjPasswordFieldMotDePasse(
			javax.swing.JPasswordField jPasswordFieldMotDePasse) {
		this.jPasswordFieldMotDePasse = jPasswordFieldMotDePasse;
	}

	public javax.swing.JTextField getjTextFieldPseudo() {
		return jTextFieldPseudo;
	}

	public void setjTextFieldPseudo(javax.swing.JTextField jTextFieldPseudo) {
		this.jTextFieldPseudo = jTextFieldPseudo;
	}

	public ClientControler getControlleur() {
		return controlleur;
	}

	public void setControlleur(ClientControler controlleur) {
		this.controlleur = controlleur;
	}
}
